<?php
namespace CFPropertyList;
$dir = __DIR__;
$pos = strrpos($dir,"/");
$rooturl = "https://smartmzinhouse.googlecode.com/git/";

$ldir = substr($dir,$pos+1);


if($argc <= 1 )
{
echo $ldir . "\r\n";
echo "use:php " . __FILE__ . " xx.ipa\r\n";
die();
}
$ipafilename = $argv[1];
if(! file_exists($ipafilename)) 
{
 die("ipa file not found:" . $ipafilename);
}
$url1 = $rooturl . $ldir . "/" .  $ipafilename;
$url2 = $rooturl . $ldir . "/icon512x512.png";
$url3 = $rooturl . $ldir . "/icon57x57.png";
$zip = zip_open($ipafilename);
if ($zip)
  {
  while ($zip_entry = zip_read($zip))
    {
	$filename = zip_entry_name($zip_entry);
	if(strstr($filename , "Info.plist"))
	{

    if (zip_entry_open($zip, $zip_entry))
      {
      // some code

		$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
		$fp = fopen ("Info.plist","w+");
		fwrite($fp,$buf);
		zip_entry_close($zip_entry);
		fclose($fp);
      }
	}

	//echo $filename . "\r\n";
  }

zip_close($zip);


// just in case...
error_reporting( E_ALL );
ini_set( 'display_errors', 'on' );

/**
 * Require CFPropertyList
 */
require_once('CFPropertyList.php');


/*
 * create a new CFPropertyList instance which loads the sample.plist on construct.
 * since we know it's a binary file, we can skip format-determination
 */


$mt = mime_content_type(__DIR__.'/Info.plist');


$mti = strstr($mt,"xml") ? 0: 1;

if($mti == 0) {
	$plist = new CFPropertyList( __DIR__.'/Info.plist', CFPropertyList::FORMAT_XML );
}
else
	$plist = new CFPropertyList( __DIR__.'/Info.plist', CFPropertyList::FORMAT_BINARY );

/*
 * retrieve the array structure of sample.plist and dump to stdout
 */

//echo '<pre>';
//var_dump( $plist->toArray() );
//echo '</pre>';
$plista = $plist->toArray();
echo "CFBundleDisplayName:" . $plista["CFBundleDisplayName"] . "\r\n";
echo "CFBundleIdentifier:" . $plista["CFBundleIdentifier"]. "\r\n";
echo "CFBundleVersion:" . $plista["CFBundleVersion"] . "\r\n";
//echo "CFBundleShortVersionString:" . $plista["CFBundleShortVersionString"] . "\r\n";

$plist2 = new CFPropertyList( __DIR__.'/smartmzinhouse.inc.plist',  CFPropertyList::FORMAT_XML );
$plist2a = $plist2->toArray();

//var_dump($plist2a);
$plist2a["items"][0]["metadata"]["subtitle"] = $plista["CFBundleDisplayName"] ;
$plist2a["items"][0]["metadata"]["title"] = $plista["CFBundleDisplayName"] ;
$plist2a["items"][0]["metadata"]["bundle-identifier"] = $plista["CFBundleIdentifier"];
$plist2a["items"][0]["metadata"]["bundle-version"] = $plista["CFBundleVersion"];
//var_dump($plist2a);
function modify_plist($plist)
{
global $plista,$url1,$url2,$url3;

foreach( $plist->getValue(true) as $key => $value )
{
	if( $value->getValue() == "@URL1@")
	{
		$value->setValue( $url1 );
	}
	
        if( $value->getValue() == "@URL2@")
        {
                $value->setValue( $url2 );
        }

	
        if( $value->getValue() == "@URL3@")
        {
                $value->setValue( $url3 );
        }

        if( $key == "subtitle" )
        {
                $value->setValue( $plista["CFBundleDisplayName"] );
        }
	else if( $key == "title" )
	{
		$value->setValue( $plista["CFBundleDisplayName"] );
	}
	else if( $key == "bundle-identifier" )
        {
                $value->setValue( $plista["CFBundleIdentifier"] );
        }
	else if( $key == "bundle-version" )
        {
                $value->setValue( $plista["CFBundleVersion"] );
        }

        if( $value instanceof \Iterator )
        {
                // The value is a CFDictionary or CFArray, you may continue down the tree
		modify_plist( $value );
        }
}



}



modify_plist($plist2);
// save data
$plist2->save( __DIR__.'/smartmzinhouse.plist', CFPropertyList::FORMAT_XML );
//$plist2->save( __DIR__.'/smartmzinhouse.plist', CFPropertyList::FORMAT_BINARY );
}
?>
